package com.example.labtest.controller;

import com.example.labtest.domain.Admin;
import com.example.labtest.domain.Student;
import com.example.labtest.domain.Student_info;
import com.example.labtest.service.AdminService;
import com.example.labtest.service.StudentInfoService;
import com.example.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;



@Controller
public class StudentInfoController {


    @Autowired
    StudentInfoService studentInfoService;

    /**
     * Model：是由SpringBoot MVC提供的，其中的内容是键值对的形式，可以存放我们的内容
     */
    @RequestMapping("/studentInfo/manageStuInfo")
    public String list(HttpServletRequest httprequest, Model model) {

        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //没有登陆，则使用redirect重定向到登录界面去登录
            return "redirect:/login/user";
        }

        /**
         * findall()方法读取admins列表里面的所有内容
         */
        List<Student_info> student_infos = studentInfoService.findAll();
        /**
         * 在model里面添加属性（键值对）
         */
        model.addAttribute("data", student_infos);
        return "/studentInfo/adlist";
    }

    @ResponseBody
    @RequestMapping("/webapi/studentInfo/findByName")
    public String findByName(String name, Model model)
    {
        if(name == null){
            name = "";
        }
        List<Student_info> students = studentInfoService.findByName(name);
        model.addAttribute("data",students);
        return "studentInfo/adlist";
    }
}
